package com.bookstore.mapper;

import com.bookstore.entity.Book;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface BookMapper {
    int insert(Book record);

    int insertSelective(Book record);

    /**
     * 获取图书的分页数据
     * @param start
     * @param pageEnd
     * @return
     */
    List<Book> getBookByPage(@Param("start") int start, @Param("pageEnd") int pageEnd);

    /**
     * 获取记录的总数
     */
    int countSize();
    /**
     * 获取记录的总数
     */
    int countSize(@Param("typeID") Integer typeID);
    /**
     * 获取前10的数据记录。
     */
    List<Book> getBookTop();

    /**
     * 获取图书的次数和总分
     */
    Book getBookNumber(@Param("bookId") String bookId);


    /**
     * 更新图书的次数和总分
     */
    int updateBookNumber(@Param("scoreNumber")Long scoreNumber,@Param("totalScore")Double totalScore,@Param("score")Double score);

    /**
     * 删除书籍
     * @return
     */
    int delBook(@Param("bookID")String bookID);

    /**
     * 参数更新数据
     */
    int updateBook(Book record);

    /**
     * 查找图书，更具图书的类型
     */
    List<Book> getBookByType(@Param("start") int start, @Param("pageEnd") int pageEnd, @Param("typeID") Integer typeID);

    List<Book> getBookByName(@Param("bookName")String bookName,@Param("start") int start, @Param("pageEnd") int pageEnd);

    int getBookByNameNumber(@Param("bookName")String bookName);

    /**
     * 根据图书分类查询图书
     * @param Cid
     */
    @Select("select BOOK_IMG,BOOK_ANTHOU,BOOK_ID，BOOK_NAME，SCORE，DOWNLOAD_NUMBER，C_NAME，NICKNAME,UPLOAD_DATE,FORMAT from " +
            "BOOK LEFT JOIN USER_ users ON UPLOAD_USER = users.U_ID LEFT JOIN CATEGORY ON TYPE_ID = C_ID WHERE TYPE_ID=#{typeID}")
    @ResultMap("simpleMSG")
    List<Book> getBookByCategory(@Param("typeID") Long Cid);

    /**
     * 根据图书的一级分类查询
     * @param Pid
     * @return
     */
    @Select("select  BOOK_IMG,BOOK_ANTHOU,BOOK_ID，BOOK_NAME，SCORE，DOWNLOAD_NUMBER，C_NAME，NICKNAME,UPLOAD_DATE,FORMAT from " +
            "BOOK LEFT JOIN USER_ users ON UPLOAD_USER = users.U_ID LEFT JOIN CATEGORY ON TYPE_ID = C_ID WHERE TYPE_ID in (select c_id from category where parent_id = #{typeID})")
    @ResultMap("simpleMSG")
    List<Book> getBookByParentCategory(@Param("typeID") Long Pid);

    /**
     * 根据评分获取前十条记录
     * @return
     */
    @Select("select  BOOK_IMG,BOOK_ANTHOU,BOOK_ID，BOOK_NAME，SCORE，DOWNLOAD_NUMBER，C_NAME，NICKNAME,UPLOAD_DATE,FORMAT from " +
            "BOOK LEFT JOIN USER_ users ON UPLOAD_USER = users.U_ID LEFT JOIN CATEGORY ON TYPE_ID = C_ID WHERE ROWNUM < 10 ORDER BY SCORE DESC ")
    @ResultMap("simpleMSG")
    List<Book> getBookOrderByScore();

    /**
     * 根据上传日期获取前十条记录
     * @return
     */
    @Select("select  BOOK_IMG,BOOK_ANTHOU,BOOK_ID，BOOK_NAME，SCORE，DOWNLOAD_NUMBER，C_NAME，NICKNAME,users.PHOTO,UPLOAD_DATE,FORMAT from " +
            "BOOK LEFT JOIN USER_ users ON UPLOAD_USER = users.U_ID LEFT JOIN CATEGORY ON TYPE_ID = C_ID WHERE ROWNUM < 10 ORDER BY UPLOAD_DATE DESC ")
    @ResultMap("simpleMSG")
    List<Book> getBookOrderByUpLoadDate();

    /**
     * 根据下载量获取前十条记录
     * @return
     */
    @Select("SELECT BOOK_IMG,BOOK_ANTHOU,BOOK_ID，BOOK_NAME，SCORE，DOWNLOAD_NUMBER，C_NAME，NICKNAME,PHOTO,UPLOAD_DATE, FORMAT from " +
            "BOOK LEFT JOIN USER_ users ON UPLOAD_USER = users.U_ID LEFT JOIN CATEGORY ON TYPE_ID = C_ID WHERE ROWNUM < 10 ORDER BY DOWNLOAD_NUMBER DESC ")
    @ResultMap("simpleMSG")
    List<Book> getBookOrderByDownloadNumber();

    /**
     * 根据图书的id查询图书
     * @param bid
     * @return
     */
    @Select("SELECT BOOK_IMG,BOOK_ANTHOU,BOOK_DETAILS，BOOK_ID，BOOK_NAME，SCORE，SCORE_NUMBER,TOTAL_SCORE，DOWNLOAD,DOWNLOAD_NUMBER，C_NAME，NICKNAME，UPLOAD_DATE, FORMAT FROM " +
            "BOOK LEFT JOIN USER_ users ON UPLOAD_USER = users.U_ID LEFT JOIN CATEGORY ON TYPE_ID = C_ID WHERE BOOK_ID=#{bid}")
    @ResultMap("BaseResultMap")
    Book getBookById(@Param("bid") String bid);

    List<Book> getMaxCommentsBook(@Param("userId")Long userId);

    List<Book> getBookIds(@Param("userId")Long userId);

    Book getModifyMessage(@Param("bookId")String bookId);
}